<?php

namespace App\Library\Traits\Orm;

use App\Models\ProductSpecModel;
use App\Models\ShopCategoryModel;
use App\Models\ShopImagesModel;
use App\Models\ShopProductExtModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

trait ShopProductOrmTrait
{
    public function specs(): HasMany
    {
        return $this->hasMany(ProductSpecModel::class, 'product_id', 'product_id')
            ->whereNull('deleted_at')->orderBy('ps_sort');
    }

    public function buyWillSpecs(): HasMany
    {
        return $this->hasMany(ProductSpecModel::class, 'product_id', 'product_id')
            ->where('ps_is_delete', 2)
            ->where('buy_will_price', '>', 0)
            ->orderBy('ps_sort');
    }

    //原价最大金额
    public function maxSpecNormalPrice(): HasOne
    {
        return self::hasOne(ProductSpecModel::class, 'product_id', 'product_id')->orderByDesc('ps_price_underlined')
            ->where('ps_is_delete', 2)->select('ps_price_underlined', 'product_id');
    }

    //原价最小金额
    public function minSpecNormalPrice(): HasOne
    {
        return self::hasOne(ProductSpecModel::class, 'product_id', 'product_id')->orderBy('ps_price_underlined')
            ->where('ps_is_delete', 2)->select('ps_price_underlined', 'product_id');
    }

    //优惠最大金额
    public function maxSpecPrice(): HasOne
    {
        return self::hasOne(ProductSpecModel::class, 'product_id', 'product_id')->orderByDesc('ps_price')
            ->where('ps_is_delete', 2)->select('ps_price', 'product_id');
    }

    //优惠最小金额
    public function minSpecPrice(): HasOne
    {
        return self::hasOne(ProductSpecModel::class, 'product_id', 'product_id')->orderBy('ps_price')
            ->where('ps_is_delete', 2)->select('ps_price', 'product_id');
    }


    public function categoryOne(): BelongsTo
    {
        return self::belongsTo(ShopCategoryModel::class, 'product_category_id_1', 'category_id')
            ->select('category_id', 'category_name');
    }

    public function categoryTwo(): BelongsTo
    {
        return self::belongsTo(ShopCategoryModel::class, 'product_category_id_2', 'category_id')
            ->select('category_id', 'category_name');
    }

    /**
     *
     * @return BelongsTo
     *
     */
    public function categoryThree(): BelongsTo
    {
        return self::belongsTo(ShopCategoryModel::class, 'product_category_id_3', 'category_id')
            ->select('category_id', 'category_name');
    }

    /**
     * 商品主图
     * @return HasMany
     *
     */
    public function avatarImages(): HasMany
    {
        return self::hasMany(ShopImagesModel::class, 'img_source_id', 'product_id')
            ->where('img_source_type', 4)->orderByDesc('img_type')->orderBy('img_sort')->select('img_id', 'img_url', 'img_type');
    }

    /**
     * 商品详情图
     * @return HasMany
     *
     */
    public function contentImages(): HasMany
    {
        return self::hasMany(ShopImagesModel::class, 'img_source_id', 'product_id')
            ->where('img_source_type', 5)->orderBy('img_sort')->select('img_id', 'img_url');
    }

    public function specPlug(): HasMany
    {
        return $this->hasMany(ProductSpecModel::class, 'product_id', 'product_id')->whereNull('deleted_at')
            ->where('ps_total_stock', 1)->whereNotNull('ps_price')->where('ps_price', '>', 0)
            ->orderBy('ps_sort')->select([
                'product_id',
                'serial_number',
                'ps_avatar',
                'ps_name',
                'ps_price',
                'ps_price_underlined',
                'ps_usable_stock',
            ]);
    }

    public function ext(): HasOne
    {
        return self::hasOne(ShopProductExtModel::class, 'product_id', 'product_id');
    }
}
